//
//  GJTempInfoModel.m
//  YDGJ
//
//  Created by 黄卫 on 17/2/24.
//  Copyright © 2017年 huangwei. All rights reserved.
//

#import "TYTempInfoModel.h"
#import "TYGlobalManager.h"

@implementation TYTempInfoModel

//警告:这个宏实现了NSCoding协议,不能删除
MJExtensionCodingImplementation


#pragma mark -================ 操作临时缓存信息, 增删查改 ================

/**
 *  添加临时缓存信息Model到单例
 */
+ (void)addTempInfoModel:(TYTempInfoModel *)tempInfoModel
{
    if (!tempInfoModel) return;
    
    [self removeTempInfoModelByUserID:tempInfoModel.userID];
    
    [[TYGlobalManager shareInstance].tempInfoModelArr addObject:tempInfoModel];
    
    //保存单例到数据库
    [TYGlobalManager saveGlobalManagerToFMDB];
}


/**
 *  根据UserID从单例移除临时缓存信息Model
 *
 *  @return 移除
 */
+ (BOOL)removeTempInfoModelByUserID:(NSString *)infoModelUserID
{
    BOOL isSuccess = NO;
    for (TYTempInfoModel *infoModel in [TYGlobalManager shareInstance].tempInfoModelArr) {
        if ([infoModel.userID isEqualToString:infoModelUserID]) {
            
            //1.从单例数组中移除
            [[TYGlobalManager shareInstance].tempInfoModelArr removeObject:infoModel];
            
            //2.保存单例到数据库
            [TYGlobalManager saveGlobalManagerToFMDB];
            isSuccess = YES;
            break;
        }
    }
    return isSuccess;
}


/**
 *  根据UserID获取临时缓存信息Model
 */
+ (TYTempInfoModel *)getTempInfoModelByUserID:(NSString *)infoModelUserID
{
    for (TYTempInfoModel *infoModel in [TYGlobalManager shareInstance].tempInfoModelArr) {
        if ([infoModel.userID isEqualToString:infoModelUserID]) {
            return infoModel;
        }
    }
    return nil;
}


/**
 *  修改临时缓存信息Model
 */
+ (void)updateTempInfoModel:(TYTempInfoModel *)tempInfoModel
{
    if (!tempInfoModel) return;
    
    for (TYTempInfoModel *infoModel in [TYGlobalManager shareInstance].tempInfoModelArr) {
        if ([infoModel.userID isEqualToString:tempInfoModel.userID]) {
            
            //1.从单例数组中移除
            [[TYGlobalManager shareInstance].tempInfoModelArr removeObject:infoModel];
            
            //2.添加新的临时缓存信息到单例
            [[TYGlobalManager shareInstance].tempInfoModelArr addObject:tempInfoModel];
            
            //3.保存单例到数据库
            [TYGlobalManager saveGlobalManagerToFMDB];
            
            break;
        }
    }
}

@end
